<?php
/**
 * Created by dichaofeng.
 * desc: 实现分页显示
 * 访问地址请使用  /c/a?page=1 的方式访问
 * Date: 2016/8/6
 * Time: 14:27
 */
class Pages
{
    public $url="";  //分页baseurl
    public $page=""; //当前页码
    public $count=0; //总共页数
    public $pagesize=20; //单页显示数量
    public $var_page="page";  //参数分割
    function __construct($page,$count,$pagesize=20,$url="",$var_page="page",$div="padding:0px 0px 0px 0px;") { //实例化并且注入分页参数 div 上右下左 距离
        $this->page=$page;
        $this->count=$count;
        $this->pagesize=$pagesize;
        $this->var_page=$var_page;
        $this->div=$div;
        if($url){
            $this->url=$url;
        }else{
            $this->url=isset($_SERVER['REDIRECT_URL'])?$_SERVER['REDIRECT_URL']:'';
        }

//            if($_SERVER['REDIRECT_QUERY_STRING']){
//                $this->url.="&";
//            }else{
//                $this->url.="?";
//            }
        // REDIRECT_URL
        $i=0;
        $parm=$_GET;
        if(isset($parm['page'])){
            unset($parm['page']);
        }
        if(is_array($parm)&&count($parm)>0){
            foreach($parm as $k=>$v){
                if($i==0){
                    if($k!="page"){
                        $this->url.="?$k=$v";
                    }

                }else{
                    if($k!="page"){
                        $this->url.="&$k=$v";
                    }
                }
                $i++;
            }
            $this->url.="&";
        }else{
            $this->url.="?";
        }
    }
    public function getView(){
        $css_class="<style>.public_fanye p span{ color:#000; border:1px solid #ccc;text-align:center; margin-right:5px; display:inline;  background:#eeeeee;}
.public_fanye p{ text-align:center;{$this->div}}
.public_fanye{padding-bottom: 25px;padding-top: 25px;}
.public_fanye span.more{ border:0; background:none; float:none;}
.public_fanye span.more:hover{ border:0; background:none; color:#000;}
.public_fanye span a{ color:#000;padding:4px 10px;}
.public_fanye span:hover,.public_fanye span.default{ color:#000; background:#c9c9c9;}</style>";

        $var_page=$this->var_page;

        $allpage = ceil($this->count/$this->pagesize);

        $cur_tag_class="";
        $prev_page=$this->page-1;
        $next_page=$this->page+1;
        //$debug_page="debug:".$this->count."/".$this->pagesize."共:".$allpage."页";
        //$debug_page="共:".$allpage."页";
        if($this->page<2){
            $prev="<span><a  href=\"javascript:void(0)\">&lt;上一页</a></span>";
        }else{
            $prev="<span><a href=\"{$this->url}$var_page={$prev_page}\">&lt;上一页</a></span>";
        }
        $tag="";

        if($allpage>6){
            for($i=1;$i<6;$i++){
                if($i==$this->page){
                    $cur_tag_class=" class=\"default\"";
                }else{
                    $cur_tag_class="";
                }
                $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page={$i}\">$i</a></span>";
            }

            $cur_tag_class="";
            if($this->page==5&&$this->page<$allpage-1){
                $tag = $tag . "<span><a href=\"{$this->url}$var_page=6\">6</a></span>";
            }
            if($this->page>5&&$this->page<$allpage){

                if($this->page!=6){
                    if($this->page!=7) {
                        $tag = $tag . "<span class=\"more\">...</span>";
                    }
                    $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page=".($this->page-1)."\">".($this->page-1)."</a></span>";
                }
                $tag=$tag."<span \" class=\"default\"><a href=\"{$this->url}$var_page={$this->page}\">$this->page</a></span>";
                if($this->page!=$allpage-1){
                    $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page=".($this->page+1)."\">".($this->page+1)."</a></span>";
                    if($this->page!=$allpage-2){
                        $tag=$tag."<span class=\"more\">...</span>";
                    }

                }

            }else{
                $tag=$tag."<span class=\"more\">...</span>";
                $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page=".($allpage-1)."\">".($allpage-1)."</a></span>";
            }



            if($allpage==$this->page){
                $cur_tag_class=" class=\"default\"";
                $next="<span><a  href=\"javascript:void(0)\">下一页&gt;</a></span>";
            }else{
                $cur_tag_class="";
                $next="<span><a href=\"{$this->url}$var_page={$next_page}\">下一页&gt;</a></span>";
            }
            $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page={$allpage}\">$allpage</a></span>";


        }else{
            for($i=1;$i<=$allpage;$i++){
                if($i==$this->page){
                    $cur_tag_class=" class=\"default\"";
                }else{
                    $cur_tag_class="";
                }
                $tag=$tag."<span$cur_tag_class><a href=\"{$this->url}$var_page={$i}\">$i</a></span>";
            }
            if($allpage==$this->page){

                $next="<span><a  href=\"javascript:void(0)\">下一页&gt;</a></span>";
            }else{
                $next="<span><a href=\"{$this->url}$var_page={$next_page}\">下一页&gt;</a></span>";
            }

        }





        //   $first = "<a href='".$this->url.'/page/1'.$this->status."' id='page_index'>首页</a>";
        //   $last  = "<a href='".$this->url.'/page/'.$allpage.$this->status."' id='page_end'>末页</a>";

        $html = $css_class."<div class=\"public_fanye\"><p>".$prev.$tag.$next."</p></div>";

        return $this->count?$html:"";
    }

}
